package com.yl.student.handler;

import com.yl.student.AppContent;
import com.yl.student.bean.dto.DataVerifyResponseDto;
import com.yl.student.bean.entity.LoginInfo;
import com.yl.student.bean.entity.ShopInfo;
import com.yl.student.bean.entity.UserInfo;
import com.yl.student.service.LoginInfoService;
import jakarta.transaction.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@Slf4j
public class PhoneRegisterHandler {
    @Resource
    private LoginInfoService loginInfoService;


    /**
     * 如果Mapping中的请求路径中传递的参数的参数名和实体类中的属性名称相同，则将参数值自动解析到该类属性中
     * @return
     * transactional：设置无论发生什么异常，都会回滚
    */
    @Transactional(rollbackOn = {Exception.class})
    @PostMapping("/registerUser")
    @ResponseBody
    public DataVerifyResponseDto registerUser(LoginInfo loginInfo, UserInfo userInfo){

        DataVerifyResponseDto dataVerifyResponseDto = null;
        //插入成功
        if (loginInfoService.registerUser(loginInfo,userInfo)) {
            dataVerifyResponseDto = new DataVerifyResponseDto("已通过","200","NULL","用户注册成功");
        }else{
            if(AppContent.isUserUserIdExit){
                dataVerifyResponseDto = new DataVerifyResponseDto("未通过","500","用户名已存在","NULL");
            }else{
                dataVerifyResponseDto = new DataVerifyResponseDto("未通过","500","用户注册失败","NULL");
            }
        }
        return dataVerifyResponseDto;
    }

    @Transactional(rollbackOn = {Exception.class})
    @PostMapping("/registerShop")
    @ResponseBody
    public DataVerifyResponseDto shopRegister(LoginInfo loginInfo,ShopInfo shopInfo){
        DataVerifyResponseDto dataVerifyResponseDto = null;
        //插入成功
        if (loginInfoService.registerShop(loginInfo,shopInfo)) {
            dataVerifyResponseDto = new DataVerifyResponseDto("已通过","200","NULL","店铺注册成功");
        }else{
            if(AppContent.isShopUserIdExit){
                dataVerifyResponseDto = new DataVerifyResponseDto("未通过","500","店铺账户号存在","NULL");
            }else{
                dataVerifyResponseDto = new DataVerifyResponseDto("未通过","500","店铺注册失败","NULL");
            }
        }
        return dataVerifyResponseDto;
    }
}
